<?php


class category_module extends CI_Module {
	public function init() {
	}
	
	public function active() {
		
	}
	
	public function deactive() {
		
	}
	
	public function update($data) {
		// if (isset($data['cateid']) && $data['cateid']) {
			// $data['cateid'] = implode(',', $data['cateid']);
		// }else {
			// $data['cateid']  = '';
		// }
		return $data;
	}
	
	public function admin($config) {
		$this->load->model('mcategory');
		$config['cat_tree'] = $this->mcategory->get_cat_tree();
		// if (isset($config['config']['cateid']) && $config['config']['cateid'])
			// $config['config']['cateid'] = explode(',', $config['config']['cateid']);
		$this->view('admin', $config);
	}
	
	/**
	 * 
	 * @param object $config
	 * 
	 */
	public function run($config) {
		$view = (isset($config['view'] ) && $config['view']) ? $config['view'] : 'index';
		$num = (isset($config['num'] ) && $config['num']) ? $config['num'] : 10;
		$config['link'] = (isset($config['link'] ) && $config['link']) ? $config['link'] : '';
		if (!(isset($config['prefix'] ) && $config['prefix']) ) 
			$config['prefix']  = '';
		switch ($view) {
			case 'index' :
				break;
			case 'mainmenu' :
				if (isset($config['cateid']) && $config['cateid']) {
					$config['cateid'] = explode(',', $config['cateid']);
					
					$config['cates'] = $this->mcategory->where_in($config['cateid'], 'cat_id');
					$cates = array();
					$n = count ($config['cateid']);
					for ($i=0; $i<$n; $i++) {
						foreach ($config['cates']  as $cat){
							if ($cat->cat_id == $config['cateid'][$i]) {
								$cates[] = $cat;
							}
						}
					}
					$config['cates'] = $cates;
				}else {
					$this->db->order_by('cat_order asc');
					$config['cates'] = $this->mcategory->gets(array(
						'cat_parent' => 0,
						'cat_status' => 1
					));
				}
				
				break;
			case "footmenu":
				if (isset($config['cateid']) && $config['cateid']) {
					
					$this->db->where_in('cat_id', explode(',', $config['cateid']));
				}else if (isset($config['cateid_src']) && $config['cateid_src']){
					$this->db->where_in('cat_id', explode(',', $config['cateid_src']));
				}
				break;
			case 'home' :
				if (!isset($config['cateid']))
				return;
				if (isset($config['cateid']) && $config['cateid']) {
					$config['cateid'] = explode(',', $config['cateid']);
					}else {
					$config['cateid']  = '';
				}
				if (!$config['cateid']) 
				return ;
				$this->db->order_by('cat_order asc');
				$config['cate'] = $this->mcategory->where_in($config['cateid'], 'cat_id');
				$this->db->limit($num);
				$this->db->order_by('game.game_id desc');
				$this->db->where('game_status >', 0);
				$config['games'] = $this->mgame->get_by_cate($config['cateid']);
				if (count($config['cateid']) == 1 && isset($config['cate']) && count($config['cate']) ==1) {
					$config['link'] =  category_url($config['cate'][0]->cat_code);
				}
				break;
			case 'parent' : 
				if (!isset($config['cateid']))
				return;
				$this->db->order_by('cat_order asc');
				$config['cate'] = $this->mcategory->get($config['cateid']);
				$this->db->limit($num);
				$this->db->order_by('game.game_id desc');
				$this->db->where('game_status >', 0);
				$config['games'] = $this->mgame->get_by_cate($config['cateid']);
				break;
		} 
		
		$this->view($view, $config);
	}
	
	
	public function plusAction($param) {
		echo $param[0] + $param[1];
	}
}